﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 通用查询窗口 : Form
    {
        //public static CreateParams(
        public 通用查询窗口()
        {
            InitializeComponent();
            //员工账号
            //充值记录表

            //用户简
            m_addItems[0] = new List<string>();
            m_addItems[0].Add("账号");m_addItems[0].Add("密码");
            m_addItems[0].Add("姓名");m_addItems[0].Add("手机号");
            m_addItems[0].Add("短手机号");m_addItems[0].Add("总页数");
            m_addItems[0].Add("创建时间");

            if (老板模式.isBossMode)
            {

                //员工操作记录
                m_addItems[1] = new List<string>();
                m_addItems[1].Add("操作员账号"); m_addItems[1].Add("员工手机号");
                m_addItems[1].Add("操作员姓名");

                //员工账号
                m_addItems[2] = null;
            }

            //充值记录表
            m_addItems[3] = new List<string>();
            m_addItems[3].Add("会员账户"); m_addItems[3].Add("会员姓名");
            m_addItems[3].Add("时间"); m_addItems[3].Add("操作员姓名");
            m_addItems[3].Add("手机号");

            //打印记录
            m_addItems[4] = new List<string>();
            m_addItems[4].Add("文档"); m_addItems[4].Add("会员姓名");
            m_addItems[4].Add("打印类型"); m_addItems[4].Add("会员账号");
            m_addItems[4].Add("单双面"); m_addItems[4].Add("打印机名");
            m_addItems[4].Add("纸张类型");

            //客户操作记录
            m_addItems[5] = new List<string>();
            m_addItems[5].Add("操作类型"); m_addItems[5].Add("详细操作");
            m_addItems[5].Add("客户姓名"); m_addItems[5].Add("客户账号");
        }

        private void 通用查询窗口_Load(object sender, EventArgs e)
        {
            dateTimePicker起始日期.Value = new DateTime(2017, 6, 1);
            dateTimePicker结束日期.Value = DateTime.Now;
            comboBox1.SelectedIndex = 0;
            //comboBox关键字.Items.Add
        }
        private List<string>[] m_addItems = new List<string>[6];
        private string sqlWhere = "";
        private string tableName = "会员简";
        private void button1_Click(object sender, EventArgs e)
        {
            tableName = comboBox1.SelectedItem.ToString();
            string str = "";

            //判断老板模式

            if ((comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 2)&&(!老板模式.isBossMode))
            {
                MessageBox.Show("非老板模式不能查询这个字段!");
                return;
            }

            if (comboBox1.SelectedIndex == 0)
            {
                if (textBox关键字.Text != null && textBox关键字.Text != "")
                {
                    str = string.Format("where {0} = '{1}' and 创建时间 between '{2}' and '{3}'"
                        , comboBox关键字.SelectedItem.ToString(), textBox关键字.Text
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    str = string.Format("where 创建时间 between '{0}' and '{1}'"
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
            }
            if (comboBox1.SelectedIndex == 3)
            {
                if (textBox关键字.Text != null && textBox关键字.Text != "")
                {
                    str = string.Format("where {0} = '{1}' and 时间 between '{2}' and '{3}'"
                        , comboBox关键字.SelectedItem.ToString(), textBox关键字.Text
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    str = string.Format("where 时间 between '{0}' and '{1}'"
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));

                }
            }
            if (comboBox1.SelectedIndex == 2)
            {
                if (textBox关键字.Text != null && textBox关键字.Text != "")
                {
                    str = "";
                    /*
                    str = string.Format("where {0} = '{1}'"
                        , comboBox关键字.SelectedItem.ToString(), textBox关键字.Text);
                     */
                }
            }
            if (comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 4)
            {
                if (textBox关键字.Text != null && textBox关键字.Text != "")
                {
                    str = string.Format("where {0} = '{1}' and 时间 between '{2}' and '{3}'"
                        , comboBox关键字.SelectedItem.ToString(), textBox关键字.Text
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    str = string.Format("where 时间 between '{0}' and '{1}'"
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));

                }
            }
            if (comboBox1.SelectedIndex == 5)
            {
                if (textBox关键字.Text != null && textBox关键字.Text != "")
                {
                    str = string.Format("where {0} = '{1}' and 时间 between '{2}' and '{3}'"
                        , comboBox关键字.SelectedItem.ToString(), textBox关键字.Text
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    str = string.Format("where 时间 between '{0}' and '{1}'"
                        , dateTimePicker起始日期.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        , dateTimePicker结束日期.Value.ToString("yyyy-MM-dd HH:mm:ss"));

                }
            }
            sqlWhere = str;
            DataSet req;
            DataBase.Error er = DataBase.getInstance().selectTable(tableName, sqlWhere, out req);
            if (er == DataBase.Error.nothing)
            {
                MessageBox.Show("没有符合条件的结果!");
                return;
            }
            dataGridView1.DataSource = req.Tables[0].DefaultView;

            var page = 0;
            var money = 0.0;
            //判断是否为老板模式，如果不是，将密码隐藏
            if (!老板模式.isBossMode && comboBox1.SelectedIndex == 0)
            {
                //不是老板模式，隐藏密码
                for(int i = 0;i<req.Tables[0].Rows.Count;i++)
                    req.Tables[0].Rows[i][2] = "********";
            }
            // 提示使用者特殊功能
            if (comboBox1.SelectedIndex == 0)
            {
                dataGridView1.Columns[0].HeaderText = "编号[双击可删除指定会员]";
                dataGridView1.Columns[1].HeaderText = "账号[双击可修改信息，需要老板模式]";
            }
            if (comboBox1.SelectedIndex == 4)
            {
                int i = 0;
                for (i = 0; i < req.Tables[0].Rows.Count; i++)
                {
                    money += Convert.ToDouble(req.Tables[0].Rows[i][6].ToString());
                    page += Convert.ToInt32(req.Tables[0].Rows[i][15].ToString());
                }

                string[] insert = new string[16];
                for (int j = 0; j < 16; j++)
                {
                    if (j == 6)
                        insert[j] = "";
                    else if (j == 15)
                        insert[j] = "";
                    else
                        insert[j] = "";
                }
                dataGridView1.Columns[6].HeaderText = "花费[" + money.ToString("0.00") + "]";
                dataGridView1.Columns[6].DefaultCellStyle.BackColor = Color.SpringGreen;
                dataGridView1.Columns[15].HeaderText = "总页数[" + page + "]";
                dataGridView1.Columns[15].DefaultCellStyle.BackColor = Color.Violet;
                //req.Tables[0].Rows.Add(insert);
                //((DataTable)((DataView)dataGridView1.DataSource.)).Rows.Add(insert); // .Rows.Add();

            }

            if (comboBox1.SelectedIndex == 0 && comboBox关键字.Text == "账号")
            {
                var hint = string.Format("您的余额为{0}元\r\n请您及时充值！\r\n余额不足无法打印！！",
                    req.Tables[0].Rows[0][4].ToString());
                Clipboard.SetDataObject(hint);
                MessageBox.Show("复制到剪贴板成功!");
            }
            
            if (comboBox1.SelectedIndex == 3)
            {
                int i = 0;
     
                for (i = 0; i < req.Tables[0].Rows.Count; i++)
                {
                    money += Convert.ToDouble(req.Tables[0].Rows[i][2].ToString());
                }

                string[] insert = new string[16];
                for (int j = 0; j < 16; j++)
                {
                    if (j == 6)
                        insert[j] = "";
                    else if (j == 15)
                        insert[j] = "";
                    else
                        insert[j] = "";
                }
                dataGridView1.Columns[2].HeaderText = "充值金额[" + money.ToString("0.00") + "]";
                dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.SpringGreen;
                
            }

        }

        private void dateTimePicker结束日期_ValueChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var list = m_addItems[comboBox1.SelectedIndex];
            comboBox关键字.Items.Clear();
            comboBox关键字.Text = "";
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    comboBox关键字.Items.Add(list[i]);
                }
            }
            textBox关键字.Text = "";
        }

        private void comboBox关键字_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Microsoft Excel文档 | *.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //如果用户选择了保存
                string localFilePath = saveFileDialog1.FileName.ToString().Substring(saveFileDialog1.FileName.ToString().LastIndexOf("\\") + 1); //获得文件路径 
                Function.ExportExcel(localFilePath,dataGridView1);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //////////////////////
            //   通用信息修改接口  -- 双击账号且上表为“会员简”时
            //////////////////////

            if (e.ColumnIndex == 1 && comboBox1.SelectedIndex == 0 && 老板模式.isBossMode)
            {
                var 账号 = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                var 密码 = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                var 姓名 = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                var 余额 = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                var 手机号 = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                通用信息修改窗口.create(账号, 密码, 姓名, 手机号, 余额);
                button1_Click(null, null);
            }

            //////////////////////
            //    删除账号接口
            //////////////////////
            if (e.ColumnIndex == 0 && comboBox1.SelectedIndex == 0)
            {
                if (DialogResult.OK == MessageBox.Show("是否要删除" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "的所有会员信息？", "提示", MessageBoxButtons.OKCancel))
                {
                    var str = string.Format(
                    "delete from 会员简 where 编号={0}"
                    , dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                    try
                    {
                        MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, str, null);
                        button1_Click(null, null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("删除会员出错=>" + ex.Message);
                    }
                }
            }

        }
    }
}
